package com.cloudsigns.factorymanager2.engineering.adapter;

import android.content.ClipData;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/12.
 */

public class MediaForRegionDetailForManagerAdapter extends BaseAdapter {

    private Context context;
    private List<EngineeringDailyFeedBackBean> datas = new ArrayList<EngineeringDailyFeedBackBean>();
    private LayoutInflater inflater;
    private ItemClickListener itemClickListener;
    private String tag = "";

    public interface MediaItemClickListener {
        public void mediaitemClick(EngineeringDailyFeedBackBean engineeringDailyFeedBackBean);

    }

    public MediaForRegionDetailForManagerAdapter(Context context, String tag) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.tag = tag;

    }

    public void setData(List<EngineeringDailyFeedBackBean> list) {

        if (null != list) {
            this.datas.clear();
            this.datas.addAll(list);
        }

    }

    public void setItemClick(ItemClickListener clickListener) {
        this.itemClickListener = clickListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public EngineeringDailyFeedBackBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_daily_feedbak_for_engineeringinstall, null);
            holder.tv_type = convertView.findViewById(R.id.tv_type);
            holder.tv_status = convertView.findViewById(R.id.tv_status);
            holder.img_arrow_right = convertView.findViewById(R.id.img_arrow_right);
            holder.img_no_feeedback = convertView.findViewById(R.id.img_no_feeedback);
            holder.rl_item = convertView.findViewById(R.id.rl_item);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        EngineeringDailyFeedBackBean bean = datas.get(position);
        holder.rl_item.setTag(bean);

        if (bean.isNoFeedBack()) {
            holder.tv_type.setText("未反馈");
            holder.img_no_feeedback.setVisibility(View.VISIBLE);
            holder.img_arrow_right.setVisibility(View.GONE);
        } else {
            holder.img_no_feeedback.setVisibility(View.GONE);
            holder.img_arrow_right.setVisibility(View.VISIBLE);

            CommonEnum.FeedBackType type = bean.getType();
            if (null != type) {
                if (type == CommonEnum.FeedBackType.NORMAL) {
                    holder.tv_type.setText("正常反馈");
                    holder.tv_status.setText("[" + bean.getStatus() + "]");
                } else if (type == CommonEnum.FeedBackType.UNUSUAL) {
                    holder.tv_type.setText("安装异常");

//                    CommonEnum.SubType subType = bean.getSubType();
//                    if (null != subType) {
//                        if (subType == CommonEnum.SubType.DAMAGE) {
//                            holder.tv_type.setText("物流货损");
//                        } else if (subType == CommonEnum.SubType.DELAY) {
//                            holder.tv_type.setText("物流延误");
//                        } else if (subType == CommonEnum.SubType.INSTALL_UNUSUAL) {
//                            holder.tv_type.setText("安装异常");
//                        } else if (subType == CommonEnum.SubType.OMIT) {
//                            holder.tv_type.setText("物流漏货");
//                        }
//                    }

                } else if (type == CommonEnum.FeedBackType.COMPLETED) {
                    holder.tv_type.setText("完工反馈");
                }

            }


            CommonEnum.UnusualStatus unusualStatus = bean.getUnusualStatus();
            if (null != unusualStatus) {
                if (unusualStatus == CommonEnum.UnusualStatus.WAIT_CONFIRM) {
                    holder.tv_status.setText("[ 等待异常确认]");
                } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY) {
                    holder.tv_status.setText("等待异常回复");
                } else if (unusualStatus == CommonEnum.UnusualStatus.WAIT_REPLY_CONFIRM) {
                    holder.tv_status.setText("等待异常回复确认");
                } else if (unusualStatus == CommonEnum.UnusualStatus.REPLY_CONFIRM_FINISH) {
                    holder.tv_status.setText("异常回复已确认");
                }
            }
        }

        holder.rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != itemClickListener) {
                    itemClickListener.itemClick(view, position);
//                    EngineeringDailyFeedBackBean engineeringDailyFeedBackBean = (EngineeringDailyFeedBackBean) view.getTag();
//                    clickListener.feedBackitemClick(engineeringDailyFeedBackBean);
                }
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private TextView tv_type;
        private TextView tv_status;
        private ImageView img_arrow_right;
        private ImageView img_no_feeedback;
        private View rl_item;
    }
}
